

#import "ColorPickerViewController.h"
#import "ColorPickerView.h"
#import "UIColor-HSVAdditions.h"

@implementation ColorPickerViewController

@synthesize delegate;
#ifdef IPHONE_COLOR_PICKER_SAVE_DEFAULT
@synthesize defaultsKey;
#else
@synthesize defaultsColor;
#endif

NSString *keyForHue = @"hue";
NSString *keyForSat = @"sat";
NSString *keyForBright = @"bright";

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
#ifdef IPHONE_COLOR_PICKER_SAVE_DEFAULT
	NSUserDefaults *saveColors = [NSUserDefaults standardUserDefaults];
	if (defaultsKey==nil) {
        self.defaultsKey = @"";
        NSLog(@"problem 0 in ColorPickerViewController.viewDidLoad");
    }
    
    NSData *colorData= [saveColors objectForKey:defaultsKey];
    UIColor *color;
    if (colorData!=nil) {
        color = (UIColor*)[NSKeyedUnarchiver unarchiveObjectWithData:colorData];
    }
#endif

    [self moveToDefault];   // Move the crosshair to the default setting
    
    UIImage *btnCancelImage = [[UIImage imageNamed:@"close-icon.png"]autorelease];
    UIButton *btnCancel = [[UIButton buttonWithType:UIButtonTypeCustom]autorelease];
    btnCancel.bounds = CGRectMake(0, 0, btnCancelImage.size.width, btnCancelImage.size.height);
    [btnCancel setImage:btnCancelImage forState:UIControlStateNormal];
    [btnCancel addTarget:self action:@selector(cancelColorSelection) forControlEvents:UIControlEventTouchUpInside]; 
    
    UIImage *btnOkImage = [[UIImage imageNamed:@"ok-icon.png"]autorelease];
    UIButton *btnOk = [[UIButton buttonWithType:UIButtonTypeCustom]autorelease];
    btnOk.bounds = CGRectMake(0, 0, btnOkImage.size.width, btnOkImage.size.height);
    [btnOk setImage:btnOkImage forState:UIControlStateNormal];
    [btnOk addTarget:self action:@selector(chooseSelectedColor) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *bbtOk =[[[UIBarButtonItem alloc]initWithCustomView:btnOk]autorelease];
    UIBarButtonItem *bbtCancel = [[[UIBarButtonItem alloc]initWithCustomView:btnCancel]autorelease];
    NSMutableArray *tbItems = [[[toolbar items]mutableCopy]autorelease];
    [tbItems insertObject:bbtCancel atIndex:0];
    [tbItems insertObject:bbtOk atIndex:3];
    
    [toolbar setItems:tbItems animated:YES];
    
    
    
    
}

-(void) moveToDefault {
  ColorPickerView *theView = (ColorPickerView*) [self view];
  #ifdef IPHONE_COLOR_PICKER_SAVE_DEFAULT
    NSUserDefaults *saveColors = [NSUserDefaults standardUserDefaults];
    NSData *colorData= [saveColors objectForKey:defaultsKey];
    UIColor *color;
    if (colorData!=nil) {
        color = (UIColor*)[NSKeyedUnarchiver unarchiveObjectWithData:colorData];
    }
    [theView setColor:color];
  #else
    [theView setColor:defaultsColor];
  #endif
    theView.updateSwatchViewDelegate   = self;
    [self updateSwatchViewWithColor:defaultsColor];
    
}

//- (void) viewWillDisappear :(BOOL)animated { 
//
//}
- (void)updateSwatchViewWithColor:(UIColor *)color
{
    toolbar.swatchColor = color;
    [toolbar setNeedsDisplay];
}
- (UIColor *) getSelectedColor {
	return [(ColorPickerView *) [self view] getColorShown];
}

- (IBAction) chooseSelectedColor {
    [delegate colorPickerViewController:self didSelectColor:[self getSelectedColor]];
}

- (IBAction) cancelColorSelection {
    #ifdef IPHONE_COLOR_PICKER_SAVE_DEFAULT
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
	if (defaultsKey==nil) {
        defaultsKey = @"";
        NSLog(@"problem 0 in ColorPickerViewController.viewDidLoad");
    }
    
    NSData *colorData= [userDefaults objectForKey:defaultsKey];
    UIColor *color;
    if (colorData!=nil) {
        color = (UIColor*)[NSKeyedUnarchiver unarchiveObjectWithData:colorData];
    }
    
    [delegate colorPickerViewController:self didSelectColor:color];
    #else
    [self dismissModalViewControllerAnimated:YES];
    #endif
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}

// Housekeeping actions when a view as unloaded
- (void)viewDidUnload {
  // Release any retained subviews of the main view.
#if ___IPHONE_OS_VERSION_MAX_ALLOWED >= 30000
  [super viewDidUnload];  // First super, from iOS 3 on

  self.chooseButton=nil;   // Same as release, but also setting it to nil
#endif

  return;
}



- (void)dealloc {
    // Release claimed resources also
#ifdef IPHONE_COLOR_PICKER_SAVE_DEFAULT
    [defaultsKey release];
#else
    [defaultsColor release];
#endif

    [super dealloc];
}

@end
